package com.shipwe.loans.utils.dialog

import android.Manifest
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.shipwe.loans.R
import com.shipwe.loans.databinding.DialogPhotoBinding
import com.shipwe.loans.pro.ShipweDialog
import com.shipwe.loans.utils.event.AppEventPointHelper
import com.shipwe.loans.utils.event.AppEventPointType
import com.shipwe.permission.OnPermissionCallback
import com.shipwe.permission.XXPermissions

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/22 17:59
 * @Annotation：拍照、相册选择 dialog
 */
class PhotoDialog: ShipweDialog {
    private lateinit var binding: DialogPhotoBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置备注
     */
    fun setEventRemark(remark: String){
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogPhotoBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(binding.photoClose)//关闭
        setOnClickListener(binding.photoCamera)//拍照
        setOnClickListener(binding.photoAlbum)//相册选择
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        when(v!!.id){
            R.id.photoCamera -> {//拍照
                showPhotoCameraClick()
                dismiss()
            }
            R.id.photoAlbum -> {//相册选择
                showPhotoAlbumClick()
                dismiss()
            }
            else -> {
                dismiss()
            }
        }
    }

    /**
     * 拍照
     */
    private fun showPhotoCameraClick(){
        val permissions = arrayListOf(
            Manifest.permission.CAMERA
        )
        if (XXPermissions.isGranted(context,permissions)){
            if(mCallback != null){
                dismiss()
                mCallback!!.openCamera()
            }
        } else {
            XXPermissions.with(context)
                // 申请多个权限
                .permission(permissions)
                .request(object: OnPermissionCallback {
                    override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                        var permissionString = ""
                        p0.forEach {
                            permissionString = "$permissionString$it,"
                        }
                        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                        if(mCallback != null){
                            dismiss()
                            mCallback!!.openCamera()
                        }
                    }
                    override fun onDenied(p0: MutableList<String>, doNotAskAgain: Boolean) {
                        var permissionString = ""
                        p0.forEach {
                            permissionString = "$permissionString$it,"
                        }
                        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
                        XXPermissions.startPermissionActivity(context,permissions)
                    }
                })
        }
    }

    /**
     * 相册
     */
    private fun showPhotoAlbumClick(){
        if(mCallback != null){
            dismiss()
            mCallback!!.openAlbum()
        }
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }

    override fun onStart() {
        super.onStart()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_PHOTO_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_PHOTO_DISMISS,eventRemark)
    }
}